package com.fz.song.controller;

import com.fz.song.pojo.HmUser;
import com.fz.song.service.HiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class HiController {

    @Autowired
    private HiService hiService;

    /**
     * 获取数据的方法
     *
     * @param id
     * @return
     */
    @RequestMapping("/getdata")
    public Object getdata(int id) {
        //打开下面的注释就走主库查询,默认走从库
//        HintManager hintManager = HintManager.getInstance();
//        hintManager.setMasterRouteOnly();
        HmUser hmUser = hiService.selectByPrimaryKey(id);
        return hmUser;
    }

    /**
     * 设置数据,走主库
     *
     * @return
     */
    @RequestMapping("/setdata")
    public Object setdata() {
        HmUser hmUser = new HmUser();
        hmUser.setUsername(UUID.randomUUID().toString());
        hmUser.setPassword("123456");
        hiService.insertSelective(hmUser);
        return hmUser;
    }

    /**
     * 测试事务
     *
     * @return
     */
    @RequestMapping("/testTransactional")
    public Object testTransactional() {
        String s = hiService.testTransactional();
        return s;
    }
}
